//
//  DingyueCell.m
//  网易新闻
//
//  Created by dllo on 15/10/8.
//  Copyright (c) 2015年 Lx. All rights reserved.
//

#import "DingyueCell.h"
#import "Settings.h"
@interface DingyueCell()
@property(nonatomic, retain)UIButton *button1;
@property(nonatomic, retain)UIButton *button2;
@property(nonatomic, retain)UILabel *tname;
@property(nonatomic, retain)UILabel *subnum;
@property(nonatomic, retain)UILabel *title;
@property(nonatomic, retain)UILabel *digest;
@property(nonatomic, retain)UILabel *line;
@end
@implementation DingyueCell
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.backgroundColor = [[Settings shareSettings]color:WhiteToGray];
        self.selectedBackgroundView = [[[UIView alloc] initWithFrame:self.frame] autorelease];
        self.selectedBackgroundView.backgroundColor = [[Settings shareSettings]color:GrayToBlack];
        [self sendSubviewToBack:self.selectedBackgroundView];
        self.background.backgroundColor = [[Settings shareSettings]color:WhiteToLightGray];
        
        self.button1 = [UIButton buttonWithType:UIButtonTypeCustom];
        [self.button1 addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
        [self.background addSubview:self.button1];
        self.button2 = [UIButton buttonWithType:UIButtonTypeCustom];
        [self.button2 addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
        [self.background addSubview:self.button2];
        
        self.tname = [[UILabel alloc]init];
        self.tname.textColor = [[Settings shareSettings]color:BlackToWhite];
        [self.button1 addSubview:self.tname];
        [self.tname release];
        
        self.subnum = [[UILabel alloc]init];
        self.subnum.textColor = [UIColor lightGrayColor];
        self.subnum.font = [UIFont systemFontOfSize:10];
        [self.button1 addSubview:self.subnum];
        [self.subnum release];
        
        self.title = [[UILabel alloc]init];
        self.title.textColor = [[Settings shareSettings]color:BlackToWhite];
        [self.button2 addSubview:self.title];
        [self.title release];
        
        self.digest = [[UILabel alloc]init];
        self.digest.textColor = [UIColor lightGrayColor];
        self.digest.font = [UIFont systemFontOfSize:10];
        [self.button2 addSubview:self.digest];
        [self.digest release];
        
        self.line = [[UILabel alloc]init];
        self.line.backgroundColor = [UIColor lightGrayColor];
        [self.background addSubview:self.line];
        [self.line release];
        
        [self notificationHandle];
    }
    return self;
}

- (void)layoutSubviews{
    [super layoutSubviews];
    self.background.frame = CGRectMake(12, 12, 350, 140);
    self.button1.frame = CGRectMake(1, 1, 348, 68);
    self.button2.frame = CGRectMake(1, 71, 348, 68);
    self.tname.frame = CGRectMake(65, 15, 250, 20);
    self.subnum.frame = CGRectMake(65, 40, 100, 20);
    self.title.frame = CGRectMake(15, 10, 320, 25);
    self.digest.frame = CGRectMake(15, 40, 320, 20);
    self.line.frame = CGRectMake(15, 70, 320, 1);
}

- (void)setMod:(DingyueModel *)mod{
    if (_mod != mod) {
        [_mod release];
        _mod = [mod retain];
    }
    self.height = 155;
    self.tname.text = mod.tname;
    self.subnum.text = mod.subnum;
    self.title.text = mod.title;
    self.digest.text = mod.digest;
}

- (void)click:(UIButton *)button{
    if (button.frame.origin.y == 1) {
        self.buttonClick(self.mod.tid, @"tid", self.mod.alias, self.mod.tname);
    }else{
        self.buttonClick(self.mod.docid, @"docid", @"", @"");
    }
}

- (void)notificationHandle{
    [[NSNotificationCenter defaultCenter]addObserverForName:@"changeBackgroundColor" object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification *note) {
        self.backgroundColor = [[Settings shareSettings]color:WhiteToGray];
        self.background.backgroundColor = [[Settings shareSettings]color:WhiteToLightGray];
        self.selectedBackgroundView.backgroundColor = [[Settings shareSettings]color:GrayToBlack];
        self.tname.textColor = [[Settings shareSettings]color:BlackToWhite];
        self.title.textColor = [[Settings shareSettings]color:BlackToWhite];
    }];
}

- (void)dealloc{
    Block_release(self.buttonClick);
    [_title release];
    [_subnum release];
    [_tname release];
    [_digest release];
    [_line release];
    [super dealloc];
}

@end
